package com.help.help.service.impl;

import com.help.help.entity.UserEntity;
import com.help.help.service.*;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.help.help.dao.OrderSDao;
import com.help.help.entity.OrderSEntity;


@Service("orderSService")
public class OrderSServiceImpl extends ServiceImpl<OrderSDao, OrderSEntity> implements OrderSService {
     @Autowired
     private UserService userService;
     @Autowired
     private SchoolService schoolService;
     @Autowired
     private AddressService addressService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<OrderSEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        QueryWrapper<UserEntity> wrapper1=new QueryWrapper<>();
        if(!StringUtil.isNullOrEmpty(key)){
            wrapper1.eq("phone", key);
            UserEntity one = userService.getOne(wrapper1);
            if (one != null) {
                wrapper.eq("user_id", one.getId());
            } else {
                wrapper.eq("uuid", key);
            }
        }

        IPage<OrderSEntity> page = this.page(
                new Query<OrderSEntity>().getPage(params),
                wrapper
        );
        List<OrderSEntity> records = page.getRecords();
        for (OrderSEntity record : records) {
            Integer userId = record.getUserId();
            record.setUser(userService.getById(userId));
            if(record.getHelpId()!=null){
                record.setHelpUser(userService.getById(record.getHelpId()));
            }
           if(record.getAddressId()!=null){
               record.setAddress(addressService.getById(record.getAddressId()));
           }
           if(record.getSchoolId()!=null){
               record.setSchool(schoolService.getById(record.getSchoolId()));
           }

        }
        return new PageUtils(page);
    }

}